#!/bin/bash

# This script launches and halts a minimalistic omniORB name server daemon
# and an associated mapper server daemons.
# The requests should be executed on the NAMESERVICE_PORT (see below) port.

##### Things you might want to configure:

# OmniOrb4 installation directory:
OMNIORB_PATH=@OMNIORB4_DIR@
OMNINAMES_BASENAME_COMMAND=omniNames

# The name of the host of the service: usually this is a FQDN (Fully Qualified
# Domain Name) as given by the shell level command: `hostname`"."`dnsdomainname`
#SERVER_NAME=@DIET_CTEST_FQDN_HOSTNAME@
# Changed by EQ to avoid strange hostname composition on unusual Graal definition
SERVER_NAME=$HOSTNAME

# The primary port on which omniMapper listens fo IIOP requests. This is
# usually defaulted to 2809. We default it to 20809 to avoid a port bind
# rejection with a potential system owned server:
MAPPER_PORT=@DIET_CTEST_OMNIORB_MAPPER_PORT@

# The port on which NameService will be available:
NAMESERVICE_PORT=@DIET_CTEST_OMNIORB_NAMESERVICE_PORT@

# Loging related:
export OMNINAMES_LOGDIR=@DIET_CMAKE_OMNINAMES_LOGDIR@

PIDOF_COMMAND=@DIET_CTEST_PIDOF_COMMAND@
MV_COMMAND=@DIET_CTEST_MV_COMMAND@
RM_COMMAND=@DIET_CTEST_RM_COMMAND@
DATE_COMMAND=@DIET_CTEST_DATE_COMMAND@
NOHUP_COMMAND=@DIET_CTEST_NOHUP_COMMAND@

############################################################
### Making sure the basic commands are available:
if [[ ! -f $PIDOF_COMMAND ]] ; then
  echo "pidof command not available"
  exit 1
fi

if [[ ! -f $MV_COMMAND ]] ; then
  echo "mv command not available"
  exit 1
fi

if [[ ! -f $RM_COMMAND ]] ; then
  echo "rm command not available"
  exit 1
fi

OMNINAMES_COMMAND=@OMNIORB4_OMNINAMES_COMMAND@
if [[ ! -f $OMNINAMES_COMMAND ]] ; then
  echo "$OMNINAMES_COMMAND command not available"
  exit 1
fi

PID_FILENAME=$OMNINAMES_LOGDIR/$OMNINAMES_BASENAME_COMMAND.pid
DESC="omniORB name server AND a mapper server daemons"

#############################
start()
{
  if [[ ! -d "$OMNINAMES_LOGDIR" ]] ; then
    mkdir -p "$OMNINAMES_LOGDIR"
  fi
  
  # The following are generated by omniNames when using the start option:
  OMNINAMES_LOG=$OMNINAMES_LOGDIR/omninames-@DIET_CMAKE_HOSTNAME@.log
  OMNINAMES_BAK=$OMNINAMES_LOGDIR/omninames-@DIET_CMAKE_HOSTNAME@.bak

  # The following are command line redirection of stdout and sterr:
    LOG_FILENAME=$OMNINAMES_LOGDIR/OmniNames.out
  ERROR_FILENAME=$OMNINAMES_LOGDIR/OmniNames.err
  
  ### Move any previous logging and error files out of the way:
  if [[ -f $OMNINAMES_LOG ]] ; then
    $MV_COMMAND $OMNINAMES_LOG $OMNINAMES_LOG-$($DATE_COMMAND +%Y%m%d%H%M%S)
  fi
  if [[ -f $OMNINAMES_BAK ]] ; then
    $MV_COMMAND $OMNINAMES_BAK $OMNINAMES_BAK-$($DATE_COMMAND +%Y%m%d%H%M%S)
  fi
  if [[ -f $LOG_FILENAME ]] ; then
    $MV_COMMAND $LOG_FILENAME $LOG_FILENAME-$($DATE_COMMAND +%Y%m%d%H%M%S)
  fi
  if [[ -f $ERROR_FILENAME ]] ; then
    $MV_COMMAND $ERROR_FILENAME $ERROR_FILENAME-$($DATE_COMMAND +%Y%m%d%H%M%S)
  fi
  
  ### Setting up the proper arguments:
  ARGS="-start $MAPPER_PORT -ignoreport"
  ARGS="$ARGS -ORBInitRef NameService=corbaname::$SERVER_NAME:$NAMESERVICE_PORT"
  ARGS="$ARGS -ORBendPoint giop:tcp:$SERVER_NAME:$NAMESERVICE_PORT"
  # Only for java client interogations
  ARGS="$ARGS -ORBsupportBootstrapAgent 1"
  
  ### If we want the stop() function (see below) to be effective we need to
  # store  the pid of the process we are about to launch (and we store it
  # in the file $PID_FILENAME). The (tiny) problem is how to get our hands
  # on the pid of our process, when another omniNames (we are not owner of)
  # is already running. Our answer is to first collect the list of existing
  # omniNames pids [and use the -o option of pidof]. So it goes:
  OTHER_PID_FILENAME=$OMNINAMES_LOGDIR/$OMNINAMES_BASENAME_COMMAND-other.pid
  $PIDOF_COMMAND $OMNINAMES_BASENAME_COMMAND > $OTHER_PID_FILENAME
  export LD_LIBRARY_PATH=$OMNIORB_PATH/lib
  $NOHUP_COMMAND $OMNINAMES_COMMAND $ARGS > $LOG_FILENAME 2> $ERROR_FILENAME & 
  sleep 1
  if [ -n "`cat $OTHER_PID_FILENAME`" ]; then
    # They were other omniNames servers running:
    $PIDOF_COMMAND -o `cat $OTHER_PID_FILENAME` $OMNINAMES_BASENAME_COMMAND > $PID_FILENAME
  else
    # NO other omniNames server previously running:
    $PIDOF_COMMAND $OMNINAMES_BASENAME_COMMAND > $PID_FILENAME
  fi
  echo "Started $DESC with pid `cat $PID_FILENAME` "
}

stop()
{
  if [[ -f $PID_FILENAME ]] ; then
    echo "Stopping: $DESC with pid $(cat $PID_FILENAME)" &&
    kill $(cat $PID_FILENAME) >> /dev/null 2>&1 &&
    $RM_COMMAND -f $PID_FILENAME
  else
    echo "$DESC not running..."
  fi
}

restart() {
   stop
   start
}

case "$1" in
   start|stop|restart)
      $1
      ;;
   *)
      echo $"Usage: $0 {start|stop|restart}"
      exit 1
esac
